/**
 * JimBot - Java IM Bot
 * Copyright (C) 2006-2009 JimBot project
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ru.jimbot.modules;

import java.util.ArrayList;
import ru.jimbot.protocol.AbstractProtocol;
import ru.jimbot.protocol.ICQProtocol;
import ru.jimbot.protocol.MMPProtocol;
import ru.jimbot.protocol.OBIMPProtocol;
import ru.jimbot.protocol.VKProtocol;
import ru.jimbot.protocol.XMPPProtocol;
import ru.jimbot.util.MainProps;

/**
 *
 * @author Prolubnikov Dmitry
 */
public class UINmanager {

    AbstractConnection con;
    public ArrayList<String> acc = new ArrayList<String>(); // ScreenName
    private ArrayList<String> pass = new ArrayList<String>(); // Pass
    public ArrayList<Integer> protocol = new ArrayList(); //Protocol
    public ArrayList<AbstractProtocol> p = new ArrayList<AbstractProtocol>(); // ссылка на процесс
    public boolean ignoreOfflineMsg=false;
    public int timeOut=/*MainProps.getIntProperty("timeOut")*/10*1000;
   
    // Creates a new instance of UINmanager 
    public UINmanager(int[] tp, String[] ic, String[] ps, AbstractConnection c, boolean ignore, AbstractProps props, String serviceName) {
        ignoreOfflineMsg = ignore;
        con = c;
        for(int i=0;i<tp.length;i++){
            acc.add(ic[i]);
            pass.add(ps[i]);
            protocol.add(tp[i]);
        }
        for(int i=0;i<tp.length;i++) {
            addProtocol(tp[i], ic[i], ps[i], props, i);
        }
    }
    
    private void addProtocol(int type, String account, String password, AbstractProps props, int i) {
        switch(type){
                case 1:
                    ICQProtocol icq = new ICQProtocol(props);
                    icq.server = MainProps.getServer();
                    icq.port = MainProps.getPort();
                    icq.screenName = account;
                    icq.password = password;
                    icq.baseUin = account;
                    icq.serviceName = con.srv.getName();
                    icq.proxyHost = con.proxy[0];
                    icq.useProxy = !con.proxy[0].equals("");
                    try{
                        icq.proxyPort = Integer.parseInt(con.proxy[1]);
                    } catch (Exception ex){
                        icq.proxyPort=0;
                    }
                    icq.proxyUser = con.proxy[2];
                    icq.proxyPass = con.proxy[3];
                    p.add(i, icq);
                    break;
                case 2:
                    XMPPProtocol xmpp = new XMPPProtocol(props);
                    xmpp.port_1= MainProps.getIntProperty("jabber.portDefault");
                    xmpp.server_1 = MainProps.getStringProperty("jabber.serverDefault");
                    xmpp.screenName = account.split("@")[0];
                    xmpp.password = password;
                    xmpp.baseUin = account;
                    xmpp.serviceName = con.srv.getName();
                    xmpp.proxyHost_xmpp = con.proxy_jabber[0];
                    xmpp.useProxy_xmpp = !con.proxy_jabber[0].equals("");
                    try{
                        xmpp.proxyPort_xmpp = Integer.parseInt(con.proxy_jabber[1]);
                    } catch (Exception ex){
                        xmpp.proxyPort_xmpp=0;
                    }
                    xmpp.proxyUser_xmpp = con.proxy_jabber[2];
                    xmpp.proxyPass_xmpp = con.proxy_jabber[3];
                    p.add(i, xmpp);
                    break;
                case 3:
                    VKProtocol vk = new VKProtocol(props);
                    vk.screenName = account;
                    vk.baseUin = account;
                    vk.password = password;
                    vk.serviceName = con.srv.getName();
                    p.add(i, vk);
                    break;
                case 4:
                    MMPProtocol mmp = new MMPProtocol(props);
                    mmp.screenName = account;
                    mmp.baseUin = account;
                    mmp.password = password;
                    mmp.serviceName = con.srv.getName();
                    p.add(i, mmp);
                    break;
                case 5:
                    OBIMPProtocol obimp = new OBIMPProtocol(props);
                    obimp.screenName = account;
                    obimp.baseUin = account;
                    obimp.password = password;
                    obimp.serviceName = con.srv.getName();
                    p.add(i, obimp);
                    break;
            }
    }
    
    public void start() {
        if (MainProps.getBooleanProperty("pause.uin.on.off")) {
            StartUins su = new StartUins(this); 
            su.start();
        } else {
            for (int i = 0; i < count(); i++)
            ((AbstractProtocol)p.get(i)).connect();
        }
    }
    
    public void stop() {
        if (MainProps.getBooleanProperty("pause.uin.on.off")) {
            StartUins su = new StartUins(this);
            su.stop();
        }
        for(int i=0;i<count();i++){
            ((AbstractProtocol)p.get(i)).disconnect();
        }        
    }
    
    public int count(){
        return acc.size();
    }
    
    public boolean getState(int i) {
        return ((AbstractProtocol)p.get(i)).isOnLine();
    }
    
    public String getAcc(int i){
        return (String)acc.get(i);
    }
}
